package com.lbd.common.validate;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @ProjectName: gulimall
 * @Package: com.lbd.common.validate
 * @ClassName: ShowStatusValidator
 * @Author: linbd
 * @Description:
 * @Date: 2021/6/14 21:33
 */
public class ShowStatusValidator implements ConstraintValidator<ListValue,Integer> {
    private Set<Integer> sets = new HashSet<>();
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        if (vals != null && vals.length > 0) {
            for (int val : vals) {
                sets.add(val);
            }
        }
    }

    /**
     * 在这里写你的方法注释
     *
     * @param val: 要校验的值
     * @param constraintValidatorContext: 上下文容器
     * @return: boolean
     * @author: linbd
     * @date: 2021/6/14 21:35
     */
    @Override
    public boolean isValid(Integer val, ConstraintValidatorContext constraintValidatorContext) {
        if (sets.contains(val)) {
            return true;
        }
        return false;
    }
}
